<!DOCTYPE html>
<html lang="en">
<head>
    <title>警员训练主页</title>
    {% include 'admin/common/header.html' %}
    <link rel="stylesheet" href="{{ url_for('static', filename='/admin/admin/css/other/user.css') }}"/>
</head>
<body class="pear-container">
{# 查询表单 #}
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="" lay-filter="user-query-form">
            <div class="layui-form-item">
                <label class="layui-form-label">警员姓名</label>
                <div class="layui-input-inline">
                    <input type="text" name="user_name" placeholder="" class="layui-input">
                </div>
                <label class="layui-form-label">情景名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="scene_name" placeholder="" class="layui-input">
                </div>
                <label class="layui-form-label">训练项名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="train_name" placeholder="" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>

{# 用户表格 #}
<div class="user-main user-collasped">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="user-table" lay-filter="user-table"></table>
        </div>
    </div>
</div>
</body>

{# 用户修改操作 #}
<script type="text/html" id="user-bar">
    {% if authorize("atkdef_scene:edit") %}
        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="pear-icon pear-icon-edit"></i>
        </button>
    {% endif %}
</script>

{# 启动与禁用 #}
<script type="text/html" id="user-enable">
    <input type="checkbox" name="enable" value="{{ "{{ d.id }}" }}" lay-skin="switch" lay-text="启用|禁用"
           lay-filter="user-enable"
            {{ "{{# if(d.enable==1){ }} checked {{# } }}" }} />
</script>

{# 用户注册时间 #}
<script type="text/html" id="user-createTime">
    {{ '  {{layui.util.toDateString(d.start_time,  "yyyy-MM-dd HH:mm:ss")}' |safe }}}
</script>

{# 用户更新时间 #}
<script type="text/html" id="user-updateTime">
    {{ '  {{layui.util.toDateString(d.end_time,  "yyyy-MM-dd HH:mm:ss")}' |safe }}}
</script>

<script type="text/html" id="train-status">
    {{ '{{#if (d.enable == true) { }}
    <span style="color: green">训练中</span>
    {{# }else if(d.enable == false){ }}
    <span style="color: red">已结束</span>
    {{# } }}'|safe }}
</script>

{% include 'admin/common/footer.html' %}

<script>
  layui.use(['table', 'dtree', 'form', 'jquery', 'popup', 'common'], function () {
    let table = layui.table
    let form = layui.form
    let $ = layui.jquery
    let dtree = layui.dtree
    let popup = layui.popup
    let common = layui.common
    let MODULE_PATH = '/admin/scene_homepage/'

    // 表格数据
    let cols = [
      [
        { title: '警员编号', field: 'user_id', align: 'center', width: 110 },
        { title: '警员姓名', field: 'user_name', align: 'center', width: 110 },
        { title: '情景名称', field: 'scene_name', align: 'center', width: 110 },
        { title: '训练项名称', field: 'train_name', align: 'center' },
<!--        { title: '是否训练中', field: 'enable', align: 'center', templet: '#user-enable', width: 120 },-->
        { title: '开始时间', field: 'registration_time', templet: '#user-createTime', align: 'center' },
        { title: '结束时间', field: 'update_time', templet: '#user-updateTime', align: 'center' },
        { title: '训练状态',field: 'enable', templet: '#train-status', align: 'center' },
        { title: '训练详情', toolbar: '#user-bar', align: 'center', width: 130 }

      ]
    ]

    // 渲染表格数据
    table.render({
      elem: '#user-table',
      url: MODULE_PATH + 'data',
      page: true,
      cols: cols,
      skin: 'line',
      height: 'full-148',
      toolbar: '#user-toolbar', /*工具栏*/
      text: { none: '暂无人员信息' },
      defaultToolbar: [{ layEvent: 'refresh', icon: 'layui-icon-refresh' }, 'filter', 'print', 'exports'] /*默认工具栏*/
    })



    //
    $('.user-group').click(function () {
      let group = $(this).attr('user-group')
      let field = form.val('user-query-form')
      if (group === '-1') {
        field.deptId = group
        $(this).removeClass('button-default')
        $(this).prev().removeClass('button-primary')
        $(this).prev().addClass('button-default')
        $(this).addClass('button-primary')
      } else {
        field.deptId = group
        $(this).removeClass('button-default')
        $(this).next().removeClass('button-primary')
        $(this).next().addClass('button-default')
        $(this).addClass('button-primary')
      }
      window.refresh(field)
    })

    table.on('tool(user-table)', function (obj) {
      if (obj.event === 'edit') {
        window.edit(obj)
      }
    })

    table.on('toolbar(user-table)', function (obj) {
      if (obj.event === 'add') {
        window.add()
      } else if (obj.event === 'refresh') {
        window.refresh()
      } else if (obj.event === 'collasped') {
        $('.user-left').toggleClass('user-collasped')
        $('.user-main').toggleClass('user-collasped')
        table.resize()
      }
    })

    form.on('submit(user-query)', function (data) {
      window.refresh(data.field)
      return false
    })

    form.on('switch(user-enable)', function (obj) {
      let operate
      if (obj.elem.checked) {
        operate = 'enable'
      } else {
        operate = 'disable'
      }
      let loading = layer.load()
      $.ajax({
        url: '/admin/scene_homepage/' + operate,
        data: JSON.stringify({ trainId: this.value }),
        dataType: 'json',
        contentType: 'application/json',
        type: 'put',
        success: function (result) {
          layer.close(loading)
          if (result.success) {
            popup.success(result.msg)
          } else {
            popup.failure(result.msg)
          }
        }
      })
    })

    window.add = function () {
      layer.open({
        type: 2,
        title: '新增',
        shade: 0.1,
        area: ['550px', '550px'],
        content: MODULE_PATH + 'add'
      })
    }

    window.edit = function (obj) {
      layer.open({
        type: 2,
        title: '详情',
        shade: 0.1,
        area: ['100%', '100%'],
        content: '/admin/scene_realtime/details/' + obj.data['id']
      })

    }


    window.refresh = function (param) {
      table.reload('user-table', { where: param })
    }
  })
</script>
</html>